﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Algo
{
    class BinarySearch
    {
        /// <summary>
        /// 二分搜索
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="compareMethod"></param>
        /// <returns></returns>
        public static int Search(int start, int end, int[] Array,int FindNum)
        {
            if (start == end)
            {
                if (Array[start]==FindNum)
                {
                    return start;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                //start=3,end=4 ,middle=4;3
                int middle = (start + end + 1) / 2;
                if (Array[middle - 1]>FindNum) //compare返回true是>middle-1
                {
                    return Search(middle, end, Array, FindNum);
                }
                else
                {
                    return Search(start, middle - 1, Array, FindNum);
                }
            }
        }
    }
}
